package com.example.myengine.engine;

import com.example.myengine.constant.STEM;
import com.example.myengine.model.TreeRuleRich;
import com.example.myengine.request.RuleEngineResult;
import com.example.myengine.request.RuleExecuteReq;
import com.example.myengine.vo.TreeNodeVO;
import com.example.myengine.vo.TreeRootVO;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author ycd0075
 * @date 2024-02-04 10:11
 */
@Slf4j
public abstract class EngineBaseProcess implements EngineProcess {

    @Override
    public RuleEngineResult process(RuleExecuteReq matter) {
        return new RuleEngineResult();
    }

    protected TreeNodeVO engineDecisionMaker(TreeRuleRich treeRuleRich, RuleExecuteReq matter, Map<String, LogicFilter> logicFilterMap) {
        TreeRootVO treeRoot = treeRuleRich.getTreeRoot();
        Map<Long, TreeNodeVO> treeNodeMap = treeRuleRich.getTreeNodeMap();

        // 规则树根ID
        Long rootNodeId = treeRoot.getTreeRootNodeId();
        TreeNodeVO treeNodeInfo = treeNodeMap.get(rootNodeId);

        // 节点类型[NodeType]；1子叶、2果实
        while (STEM.NODE.getCode().equals(treeNodeInfo.getNodeType())) {
            String ruleKey = treeNodeInfo.getRuleKey();
            LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            String matterValue = logicFilter.matterValue(matter);
            Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLineInfoList());
            treeNodeInfo = treeNodeMap.get(nextNode);
            log.info("决策树引擎=>{} userId：{} treeId：{} treeNode：{} ruleKey：{} matterValue：{}", treeRoot.getTreeName(), matter.getUserId(), matter.getTreeId(), treeNodeInfo.getTreeNodeId(), ruleKey, matterValue);
        }

        return treeNodeInfo;
    }
}
